//
//  CLPhotoViewController.m
//  chanlin
//
//  Created by Ryan on 14-11-10.
//  Copyright (c) 2014年 chanlin. All rights reserved.
//

#import "CLPhotoViewController.h"
#import "AQRImageZoomView.h"
#import "CLGloble.h"
#import "CLSlidersViewController.h"
#import "AQRImageZoomView.h"
#import "NSObject+sagittarius.h"
#import "AQRBGView.h"
#import "UIImage+Utils.h"

@interface CLPhotoViewController ()

@property (nonatomic,unsafe_unretained)BOOL isCurrent;

@property (nonatomic,weak)CLSlidersViewController * supperController;
@property (nonatomic,assign)AQRSlidesViewStatue statue;



@end

@implementation CLPhotoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.statue=statueAQRSlideNone;
}

- (void)loadView{
    self.view=[[AQRBGView alloc] initWithFrame:CGRectMake(0, 0, GlobleWidth, GlobleHeight)];
    [((AQRBGView *)self.view).zoomView tapSelect:@selector(tap) target:self];
    
}

- (void)tap{
    //[supperController changeHidden];
    if([self.supperController respondsToSelector:@selector(changeHidden)]){
        [self.supperController performSelector:@selector(changeHidden)];
    }
}


- (void)slideDidCreate:(AQRSlidesView *) slideView param:(NSObject *)param{
    if(param && [param isKindOfClass:[CLSlidersViewController class]]){
        self.supperController=(CLSlidersViewController *)param;
    }
   
}

- (void)slideChange:(AQRSlidesView *)slideView frame:(CGRect)rect statue:(AQRSlidesViewStatue)statue ToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
//    [(AQRImageZoomView *)self.view changeToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
//                                                 WithIsInBehind:NO];
}

- (void)slideChange:(AQRSlidesView *) slideView frame:(CGRect)rect statue:(AQRSlidesViewStatue) statue{
    NSArray *array=[self.supperController slidesData];
    NSDictionary *json = nil;
    
    if(statueAQRSlideCurrent==statue){
        json=[array sgrGetDictionaryForIndex:slideView.currentIndex];
    }else if(statueAQRSlidePre==statue){
        json=[array sgrGetDictionaryForIndex:slideView.currentIndex-1];
        
    }else if(statueAQRSlideNext==statue){
        json=[array sgrGetDictionaryForIndex:slideView.currentIndex+1];
        
    }
    
    
    [((AQRBGView *)self.view).zoomView updateFrameWithRect:self.view.bounds];
    [self getImageWithDateDic:json];
    
    if ([[json sgrGetStringForKey:@"type"] isEqualToString:@"behind"]){
        
        [((AQRBGView *)self.view) hiddenImage];
        [self.view addSubview:[self.supperController getCLBehindeView]];
//        self.supperController.tool.view.alpha=1;
        [self.supperController getCLBehindeView].hidden=YES;
    }else{
        [((AQRBGView *)self.view) hiddenBehind];
//        self.supperController.tool.view.alpha=0;
        [self.supperController behindView].hidden=NO;
    }
    
  
    
//    if(self.supperController && [self.supperController respondsToSelector:@selector(sliderInterfaceOrientation)])
//        [(AQRImageZoomView *)self.view changeToInterfaceOrientation:[self.supperController sliderInterfaceOrientation] WithIsInBehind:NO];
}

- (UIImage *)currentImg{
    return [((AQRBGView *)self.view).zoomView image];
}

-(void)getImageWithDateDic:(NSDictionary *)jsonDic{
    
    //当无数据时，置空
    if (!jsonDic) {
        [((AQRBGView *)self.view).zoomView setImageNil];
        [((AQRBGView *)self.view).zoomView setImageUrlNil];
        return;
    }
    
    NSString *imgurl=[jsonDic sgrGetStringForKey:@"image"];
    imgurl = [imgurl stringByReplacingOccurrencesOfString:@"&amp;" withString:@"&"];
    if (!imgurl || [imgurl length]<5) {
        return;
    }
    __weak AQRBGView *bgv=(AQRBGView *)self.view;
            [((AQRBGView *)self.view).zoomView setImageUrl:imgurl block:^(UIImage *img) {
            if(img){
                bgv.bgImg.image=[img blurRadius:50];
         
            }
        }];
    
}

- (void)slideChange:(AQRSlidesView *) slideView statue:(AQRSlidesViewStatue) statue{
    [((AQRBGView *)self.view).zoomView resetScale];
    if(statue==statueAQRSlidePre && slideView.currentIndex==0){
        [((AQRBGView *)self.view).zoomView setImageNil];
    }else
    if(statue==statueAQRSlideCurrent){
        NSArray *array=[self.supperController slidesData];
        NSDictionary *json=[array sgrGetDictionaryForIndex:slideView.currentIndex];
        [self.supperController setTitle:json];
        [self.supperController hiddenInfo:[[json sgrGetStringForKey:@"type"] isEqualToString:@"behind"]];
    }
    
        self.statue=statue;
    
}




@end
